<?php

class DiaryCell extends PageElement
{
	protected $date;
	protected $events = array();
	protected $dataTableName;


	public function __construct($dataTableName,$date)
	{
//		echo $date;
		$this->date = $date;
		$this->dataTableName = $dataTableName;
	}

	public function addEvent($event)
	{
		$time = new DateTime($event->getStart());
//		echo $time->format("Y-m-d")."<br>";
		if($event<>'')
			array_push($this->events,$event);
	}

	public function getDate()
	{
		return $this->date;
	}

	public function getHTML()
	{


//		return "<div id=\"cell\"><p class=\"diarycelldate\" onclick=\"newDatedRecord('".$this->dataTableName."','".$this->date."');\">6</p></div>\n";
//		return "<div id=\"cell\"><p class=\"diarycelldate\"><a href=\"javascript:newDatedRecord('".$this->dataTableName."','".$this->date."')\">6</a></p></div>\n";
		$dtDay = new DateTime($this->date);
		$strDay = $dtDay->format("j");
		$html = "<div id=\"cell\">";
		$html.= "<p class=\"diarycelldate\" ";
//		$html .= "onclick=\"newDatedRecord('".$this->dataTableName."','".$this->date."');\" ";
		$html .= ">";
		$html .= "<a href = \"javascript:newDatedRecord('".$this->dataTableName."','".$this->date."')\" title = \"Create new event on ".$dtDay->format("l j F Y")."\">".$strDay."</a>";
		$html .= "</p>\n";
		foreach ($this->events as $event)
		{
			$startTime = new DateTime($event->getStart());
			$endTime = new DateTime($event->getEnd());
			$html .= "<p class=\"info\">\n";

			$html .= "<a href = \"javascript:editRecord('".$event->getEventID()."', 'Events')\">";
			$html .= "<img style=\"border: 0px\" width=\"16\" height=\"16\" src=\"../../../MSWebGUI/Images/Icons/edit-icon.png\" title=\"Edit '".$event->getTitle()."'\"\n";
//			$html .= "onclick=\"editRecord('".$event->getEventID()."', 'Events')\"\n";
			$html .= "/ >";
			$html .= "</a>";

			$html .= "<a href = \"javascript:deleteRecord('".$event->getEventID()."', 'Events')\">";
			$html .= "<img style=\"border: 0px\" width=\"16\" height=\"16\" src=\"../../../MSWebGUI/Images/Icons/edit-delete-icon.png\"  title=\"Delete '".$event->getTitle()."'\"\n";
//			$html .= "onclick=\"deleteRecord('".$event->getEventID()."', 'Events')\"\n";
			$html .= "/ >";
			$html .= "</a>&nbsp;&nbsp;";

			$html .= $startTime->format("H:i")."&nbsp;-&nbsp;".$endTime->format("H:i");
			$html .= "<br>\n";
			$html .= "<span \n";
			$html .= "onmouseover=\"showDiaryPopup('Event".$event->getEventID()."')\"\n";
			$html .= "onmouseout=\"hideDiaryPopup('Event".$event->getEventID()."')\"\n";
			$html .= " >";
			$users = $event->getUsers()->getArray();
			if($event->getDeletedBy()!= '0')
				$html .= "(Deleted by ".$users[$event->getDeletedBy()].")<br>\n";
			$html .= $event->getTitle()."</p>\n";
			$html .= "</span>\n";
			$html .= "<span class = \"diarypopup\" id = \"Event".$event->getEventID()."\">\n";
			$html .= $startTime->format("H:i")."&nbsp;-&nbsp;".$endTime->format("H:i")."<br>\n";
			$EventTypes = $event->getEventTypes()->getArray();
			if(isset($EventTypes[$event->getEventTypeID()]))
		 		$html .= $EventTypes[$event->getEventTypeID()]."<br>\n";
			$html .= $event->getTitle()."<br>\n";
//			echo $event->getDeletedBy()."<br>";
			$html .= $event->getDescription()."<br>\n";
			$html .= $event->getComment()."<br>\n";
			$html .= "<hr>\n";
			$html .= "Responsible person<br>";
			$html .= $event->getResponsible()."<br>\n";
			$html .= "<hr>\n";
			$html .= "Involving<br>";
			$involvee = $event->getEventInvolvee();
			foreach ($involvee as $theInvolvee)
				$html .= $theInvolvee." \n";
			$html .= "<hr>\n";
			$html .= "In<br>";
			$location = $event->getLocation();
			foreach ($location as $theLocation)
				$html .= $theLocation." \n";
			$html .= "<hr>\n";
			$html .= "Created by ";
			$html .= $users[$event->getCreatedBy()]."<br>\n";
			$createTime = new DateTime($event->getCreatedWhen());
			$html .= $createTime->format("H:i d/m/Y")."<br>\n";
			if($event->getDeletedBy()!=0)
			{
				$html .= "Deleted by ";
				$html .= $users[$event->getDeletedBy()]."<br>\n";
				$deleteTime = new DateTime($event->getDeletedWhen());
				$html .= $deleteTime->format("H:i d/m/Y")."<br>\n";
			}
			$html .= "</span>\n";


		}
		$html .= "</div>\n";
		return $html;
	}
}

?>